package com.treasure.web.controller.corp.admin;

import com.treasure.common.model.PageResult;
import com.treasure.common.model.SessionUser;
import com.treasure.common.model.SessionUserHolder;
import com.treasure.common.reponse.Response;
import com.treasure.web.command.request.chat.CorpChatHistoryDetailRequest;
import com.treasure.web.command.response.ConversationListResponse;
import com.treasure.web.command.response.CorpChatHistoryDetailResponse;
import com.treasure.web.command.response.SystemUploadResponse;
import com.treasure.web.mapstruct.ConversationDetailAssembler;
import com.treasure.web.mapstruct.PageResultConvert;
import com.treasure.web.service.FileUploadService;
import com.treasure.web.service.IChatHistoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * corpManage 企业管理后台
 *
 * @author sunhongmin
 * @date 2024/1/13 18:26
 * @description
 */
@RestController
@RequestMapping("/treasure/corp/manage")
@RequiredArgsConstructor
public class CorpManageController {
    private final FileUploadService fileUploadService;
    private final IChatHistoryService chatHistoryService;

    /**
     * logo文件上传
     *
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public Response<SystemUploadResponse> upload(@RequestParam(name = "file") MultipartFile file) throws IOException {
        return Response.success(fileUploadService.fileUpload(file));
    }


    /**
     * 企业对话查询
     *
     * @param corpChatHistoryDetailRequest
     * @return
     */
    @PostMapping("/conversation/list")
    public Response<PageResult<CorpChatHistoryDetailResponse>> conversationList(@RequestBody CorpChatHistoryDetailRequest corpChatHistoryDetailRequest) {
        SessionUser sessionUser = SessionUserHolder.getAndCheckSessionUser();
        PageResult<ConversationListResponse> conversationListResponsePageResult = chatHistoryService.queryChatHistoryDetailList(corpChatHistoryDetailRequest, sessionUser.getCorpId());
        PageResult<CorpChatHistoryDetailResponse> result = new PageResult<>();
        PageResultConvert.INSTANCE.convert(conversationListResponsePageResult, result);
        result.setRecords(ConversationDetailAssembler.INSTANCE.convert(conversationListResponsePageResult.getRecords()));
        return Response.success(result);
    }


}
